package com.lhg.springdemo.a01;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;

@SpringBootApplication
public class A01Application {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException {
        ConfigurableApplicationContext context = SpringApplication.run(A01Application.class,args);
        /**
         * 什么是beanFactory
         * beanFactory是ApplicationContext的父接口
         */

        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        // 设置可见性
        singletonObjects.setAccessible(true);

        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        Map<String,Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
        
        map.entrySet().stream().filter(e->e.getKey().startsWith("component")).forEach(e->{
            System.out.println("e.getValue() = " + e.getValue());
        });

        //
        System.out.println("context.getMessage(\"hi\",null, Locale.CHINA) = " + context.getMessage("hi", null, Locale.CHINA));

//        Resource[] resources = context.getResources("classpath:application.properties");

        Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource :
                resources) {
            System.out.println("resource = " + resource);
        }

    }
}
